package config;

import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jfinal.handler.Handler;
import com.jfinal.kit.StrKit;
import com.jfinal.render.RenderFactory;

public class MyUrlSkipHandler extends Handler {

    private Pattern skipedUrlPattern;

    public MyUrlSkipHandler(String skipedUrlRegx, boolean isCaseSensitive) {
        if (StrKit.isBlank(skipedUrlRegx))
            throw new IllegalArgumentException("The para excludedUrlRegx can not be blank.");
        skipedUrlPattern = isCaseSensitive ? Pattern.compile(skipedUrlRegx) : Pattern.compile(skipedUrlRegx, Pattern.CASE_INSENSITIVE);
    }

    public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
        if (skipedUrlPattern.matcher(target).matches()) {
            RenderFactory renderFactory = RenderFactory.me();
//            renderFactory.getErrorRender(401).setContext(request, response).render();
            renderFactory.getRedirectRender("/index").setContext(request,response).render();
        } else {
            next.handle(target, request, response, isHandled);
        }
    }
}